<!DOCTYPE html>
<html>
<head>
    <title>卡密-添加</title>
    {% include 'system/common/header.html' %}
</head>
<body>
<form class="layui-form">
    <div class="mainBox">
        <div class="main-container">
          <div class="layui-form-item">
              <label class="layui-form-label">项目名称</label>
              <div class="layui-input-block">
                  <input type="text" name="name" lay-verify="title" autocomplete="off"
                          placeholder="请使用英文字母，不可使用特殊字符" class="layui-input">
              </div>
          </div>
          <div class="layui-form-item">
              <label class="layui-form-label">卡号</label>
              <div class="layui-input-block">
                  <input type="text" name="card" lay-verify="title" autocomplete="off" placeholder="XXXX-XXXX-XXXX-XXXX"
                          class="layui-input">
              </div>
          </div>
          <div class="layui-form-item">
              <label class="layui-form-label">卡密</label>
              <div class="layui-input-block">
                <input type="text" name="password" lay-verify="title" autocomplete="off" placeholder="XXXX-XXXXXXXXXXXXXX"
                        class="layui-input">
              </div>
          </div>
          <div class="layui-form-item">
            <label class="layui-form-label">状态</label>
            <div class="layui-input-block">
              <select name="status" aria-labelledby="status">
                <option value="未使用">未使用</option>
                <option value="生效中">生效中</option>
                <option value="停用">停用</option>
                <option value="已过期">已过期</option>
              </select>
            </div>
          </div>
          <div class="layui-form-item">
            <label class="layui-form-label">客户端UID</label>
            <div class="layui-input-block">
              <input type="text" name="uid" lay-verify="title" autocomplete="off" placeholder="软件/硬件唯一标识码UID"
                      class="layui-input">
            </div>
          </div>
          <div class="layui-form-item">
            <label class="layui-form-label">有效期</label>
            <div class="layui-input-block">
              <input type="date" name="expire_time" lay-verify="title" autocomplete="off" placeholder="发布日期"
              class="layui-input" value="">
              <script>
                // 生成card卡号
                var card = 'L';
                var card_date = new Date();
                // 卡号格式为YYMM-DDHH-MMSS-XXXX,后面为随机数
                year=card_date.getFullYear().toString().substring(2,4); //获取年份的后两位
                month=(card_date.getMonth()+1).toString().padStart(2,'0');  //获取月份，并补0
                day=card_date.getDate().toString().padStart(2,'0');  //获取日期，并补0
                hour=card_date.getHours().toString().padStart(2,'0');  //获取小时，并补0
                minute=card_date.getMinutes().toString().padStart(2,'0');  //获取分钟，并补0
                

                card = year + getRandomString(2) + '-' +  month + getRandomString(2) + '-' + day + hour + "-" + minute + getRandomString(2);
                document.querySelector('input[name="card"]').value = card;

                // 生成password卡密
                var password = year + month + getRandomString(12);
                document.querySelector('input[name="password"]').value = password;

                function getRandomString(length) {
                  const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
                  //随机生成2个字符串
                  let result = '';
                  for (let i = 0; i < length; i++) {
                    result += characters.charAt(Math.floor(Math.random() * characters.length));
                  }
                  return result;
                }

                
                // 设置"expire_time为默认当前日期
                var date = new Date();
                // +30日
                date.setDate(date.getDate() + 30);
                document.querySelector('input[name="expire_time"]').value = date.toISOString().slice(0,10);
              </script>
            </div>
          </div>
          <div class="layui-form-item">
            <label class="layui-form-label">发卡时间</label>
            <div class="layui-input-block">
              <input type="date" name="create_time" lay-verify="title" autocomplete="off" placeholder="发布日期"
                      class="layui-input" value="">
              <script>
                // 设置create_time为默认当前日期
                document.querySelector('input[name="create_time"]').value = new Date().toISOString().slice(0,10);
              </script>
            </div>
          </div>
          <div class="layui-form-item">
            <!-- 显示二维码 卡密信息 -->
            <!-- <script src="https://cdn.jsdelivr.net/npm/qrcodejs@1.0.0/qrcode.min.js"></script> -->
            <label class="layui-form-label">卡密信息</label>
            <div class="layui-input-block">
              <div id="qrcode"></div>
              <script>
                card = document.querySelector('input[name="card"]').value;
                password = document.querySelector('input[name="password"]').value;
                // 生成二维码
                var qrcode = new QRCode(document.getElementById("qrcode"), {
                  text: "card: " + card + "\npassword: " + password,
                  width: 128,
                  height: 128,
              })
              </script>
            </div>
          </div>
        </div>
    </div>
    <div class="bottom">
        <div class="button-container">
            <button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit="" lay-filter="kami-save">
                <i class="layui-icon layui-icon-ok"></i>
                提交
            </button>
            <button type="reset" class="pear-btn pear-btn-sm">
                <i class="layui-icon layui-icon-refresh"></i>
                重置
            </button>
        </div>
    </div>
</form>
{% include 'system/common/footer.html' %}
<script>
  layui.use(['form', 'jquery'], function () {
    let form = layui.form
    let $ = layui.jquery
    
    // let MODULE_PATH = '/admin/system/kami/'
    let MODULE_PATH = "{{ url_for('system.kami.main') }}"
    form.on('submit(kami-save)', function (data) {
      let loading = layer.load()
      let formData = data.field;
      $.ajax({
        url: MODULE_PATH + 'save',
        data: JSON.stringify(data.field),
        dataType: 'json',
        contentType: 'application/json',
        type: 'post',
        success: function (result) {
          layer.close(loading)
          if (result.success) {
            layer.msg(result.msg, { icon: 1, time: 1000 }, function () {
              parent.layer.close(parent.layer.getFrameIndex(window.name))//关闭当前页
              parent.layui.table.reload('kami-dataTable')
            })
          } else {
            layer.msg(result.msg, { icon: 2, time: 1000 })
          }
        }
      })
      // console.log(data.field)
      return false
    })
  })
</script>
</body>
</html>